package code; public class DataT { int length; private char[] hanzi; private int[] index; public DataT(int k) { this.length=k; hanzi=new char[k]; index=new int[k]; } public DataT(char[] i,int k) { this.hanzi=i; this.length=k; this.index=new int[k]; } public char getbyIndex(int k) { return this.hanzi[k]; } public int findIndex(char c) { int i=0; while(i<this.length&&this.hanzi[i]!=c)i++; if(i>=this.length)return -1; return i; } public String toString() { String result=""; for(int i=0;i<this.length;i++) result+=(this.hanzi[i]+"\n"); return result; } }
package code; /* * Matrix.java * * Created on 2007年5月24日, 上午10:16 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** * * @author Administrator */ import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class Matrix { private static BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); private static PrintWriter stdOut = new PrintWriter(System.out, true); private static PrintWriter stdErr = new PrintWriter(System.err, true); // private static /** 行数 */ private int m = 0; /** 列数 */ private int n = 0; /** 储存矩阵内的数 */ public double[][] num ; /** 默认构造函数 */ public Matrix() { this.n = 0; this.m = 0; } /** * <p> * 接受两个整形参数的构造函数 * </p> * <p> * 从键盘输入矩阵的内容 * </p> */ public Matrix(int m, int n) { this.n = n; this.m = m; num = new double[m][n]; /*stdOut.println("请按从左到右,从上到下的顺序输入矩阵的内容:"); try { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { num[i][j] = Integer.parseInt(stdIn.readLine()); } } } catch (NumberFormatException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); }*/ for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { num[i][j] = 1.000/n; } } } /** 打印出矩阵的内容 */ public void print() { for (int i = 0; i < this.m; i++) { for (int j = 0; j < this.n; j++) { stdOut.print(this.getNum(i, j)); stdOut.print(" "); } stdOut.println(); } } /** 返回m的值 */ public int getM() { return this.m; } /** 返回n的值 */ public int getN() { return this.n; } /** 返回矩阵的所有内容 */ public double[][] getNum() { return this.num; } /** 返回矩阵内第m行,第n列的内容 */ public double getNum(int m, int n) { if (m < this.m && n < this.n && m >= 0 && n >= 0) { return this.num[m][n]; } else { stdErr.println("返回矩阵第" + m + "行第" + n + "列的内容时出现索引错误"); return 0; } } /** 设置m的值 */ private void setM(int m) { this.m = m; } /** 设置n的值 */ private void setN(int n) { this.n = n; } /** 设置矩阵第m行第n列的内容 */ public void setNum(double num, int i, int j) { if (i < this.m && j < this.n && i >= 0 && j >= 0) { this.num[i][j] = num; } else { stdErr.println("设置矩阵第" + i + "行第" + j + "列的内容时出现索引错误"); } } /** 两个矩阵的加法 */ public Matrix add(Matrix m) { Matrix tmp = new Matrix(); if (this.m == m.getM() && this.n == m.getN()) { tmp.setM(this.m); tmp.setN(this.n); tmp.num = new double[this.m][this.n]; for (int i = 0; i < this.m; i++) { for (int j = 0; j < this.n; j++) { tmp.setNum(this.num[i][j] + m.num[i][j], i, j); } } } else { stdErr.println("这两个矩阵不可以计算加法"); } return tmp; } /** 两个矩阵的减法 */ public Matrix sub(Matrix m) { Matrix tmp = new Matrix(); if (this.m == m.getM() && this.n == m.getN()) { tmp.setM(this.m); tmp.setN(this.n); tmp.num = new double[this.m][this.n]; for (int i = 0; i < this.m; i++) { for (int j = 0; j < this.n; j++) { tmp.setNum(this.num[i][j] - m.num[i][j], i, j); } } } else { stdErr.println("这两个矩阵不可以计算减法"); } return tmp; } /** 两个矩阵的乘法 */ public Matrix mul(Matrix m) { Matrix tmp = new Matrix(); if (this.n == m.getM()) { tmp.setM(this.m); tmp.setN(m.getN()); tmp.num = new double[this.m][m.getN()]; for (int i = 0; i < this.m; i++) { for (int j = 0; j < m.getN(); j++) { int sum = 0; for (int k = 0; k < this.n; k++) { sum += this.num[i][k] * m.getNum(k, j); } tmp.setNum(sum, i, j); } } } else { stdErr.println("这两个矩阵不可以计算乘法"); } return tmp; } /** 求行列式的aij的代数余子式 */ public Matrix det(int m, int n) { Matrix tmp = new Matrix(); if (m >= this.m || n >= this.n) { stdErr.println("求行列式的a" + m + n + "的代数余子式时发生索引错误."); return tmp; } tmp.setM(this.m - 1); tmp.setN(this.n - 1); tmp.num = new double[this.m - 1][this.n - 1]; for (int i = 0, index1 = 0; i < this.m; i++) { if ((m - 1) != i) { // for (int j = 0, index2 = 0; j < this.n; j++) { ERROR INDEX for (int j = 0, index2 = 0; j < (this.n - 1); j++) { if ((n - 1) != j) { if ((m + n) % 2 == 1) { tmp.setNum(-1 * this.num[i][j], index1, index2); } else { tmp.setNum(this.num[i][j], index1, index2); } index2++; } } index1++; } } return tmp; } /** 求行列式的值 */ public double getResult() { int result = 0; if (this.m == this.n) { if (this.m == 1) { return this.num[0][0]; } else { for (int i = 0; i < n; i++) { Matrix tmp = this.det(1, i); result += this.num[1][i] * tmp.getResult(); } return result; } } else { stdErr.println("该矩阵不是行列式,不能求值."); return 0; } } /** 求逆矩阵 */ public Matrix ath() { Matrix tmp = new Matrix(); if (this.m == this.n) { double result = this.getResult(); if (result != 0) { tmp.setM(this.m); tmp.setN(this.n); tmp.num = new double[this.m][this.n]; for (int i = 0; i < this.n; i++) { for (int j = 0; j < this.n; j++) { this.setNum(this.det(i, j).getResult() / result, i, j); } } } else { stdErr.println("行列式的值为0, 不存在逆矩阵."); } } else { stdErr.println("该矩阵不是行列式,不存在逆矩阵."); } return tmp; } /** 主方法.测试Matrix类 */ public static void main(String args[]) { Matrix m1 = new Matrix(3, 3); // Matrix m2 = new Matrix(3, 2); // Matrix m3 = m1.mul(m2); stdOut.println(m1.getResult()); } public String[] toS() { String[] r=new String[this.m]; String t=""; for(int i=0;i<this.m;i++) { t=""; for(int j=0;j<this.n;j++) { t=t+""+this.num[i][j]+","; } t+="\n"; r[i]=t; } r[1]=r[1]+""; return r; } public void saveM(String filename) throws Exception{ FileWriter fw=new FileWriter(filename);//System.out.print(dist.toString()); //String[] r=new String[this.m]; String t=""; for(int i=0;i<m;i++) { t=""; for(int j=0;j<n;j++) { t=t+""+this.num[i][j]+","; } t+="\n"; fw.write(t); System.out.println(i); //r[i]=t; } //r[1]=r[1]+""; fw.flush(); fw.close(); } public int findMaxinCol(int col) { double a=this.num[col][0]; int index=0; for(int j=1;j<this.n;j++) { index=a>=this.num[col][j]?index:j; } return index; } }
package code; import java.io.*; import java.text.DecimalFormat; public class getSource { /** * @param args */ DataT DT; //static int[][] a3=new int[2000][2000]; Matrix dist =new Matrix(2500,2500); public getSource(String filem,String filen) throws Exception{ FileReader fi=new FileReader(filem); BufferedReader br=new BufferedReader(fi); String a=(String)br.readLine(); //System.out.print(a.charAt(2)); //System.out.print(a); int i=0; while(true) { if(a.charAt(i)!='@')i++; else break; } System.out.println(i+1); double[][] dis=new double[i][]; char[] ch=new char[i]; int j=0; while(a.charAt(j)!='@')ch[j]=a.charAt(j++); DT=new DataT(ch,i); fenhua(filen); // this.dist.saveM("F.txt"); // String[] r=dist.toS(); //for(int ii=0;ii<dist.getM();ii++)System.out.print(r[ii]); //dist = new Matrix(i,i); //dist=new double[i][]; //for(int a1=0;a1<i;a1++)dist[a1]=new double[i]; /* for(int k=0;k<2000;k++)for(j=0;j<=k;j++) { dist.setNum(1/i, k, j);//dist[k][j]=1/i; dist.setNum(1/i, j, k);//dist[j][k]=1/i; }*/ //System.out.print(dt.toString()); } public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub DecimalFormat df2 = new DecimalFormat("###.00000000"); getSource gs=new getSource("hanzi.txt","wenzhang.txt"); String[] re=gs.getMostPairly(); for (int i=0;i<gs.dist.getM();i++)System.out.println(re[i]); /*for(int k=0;k<gs.dist.getM();k++) { for(int j=0;j<gs.dist.getN();j++) { System.out.print(df2.format(gs.dist.getNum(k, j))+" "); } System.out.println(); } //java.util. //double [] a1=new double[(int)Math.pow(2, 19)]; //int [] a2=new int[(int)Math.pow(2, 20)]; //System.out.print( Math.pow(2, 20)); FileReader fr=new FileReader("F.txt"); BufferedReader br=new BufferedReader(fr); System.out.print((char)br.read()+""+(char)br.read()+""+(char)br.read()+""+(char)br.read()); */ } public String[] getMostPairly() { int row=this.dist.getM(); String[] result=new String[row]; for(int i=0;i<row;i++) { String a=this.DT.getbyIndex(i)+""; a=a+this.DT.getbyIndex(this.dist.findMaxinCol(i)); result[i]=a; } return result; } public void fenhua(String file)throws Exception { FileReader fr=new FileReader(file); BufferedReader br=new BufferedReader(fr); char p1=(char)br.read(); char p2=(char)br.read(); while(br.ready()) { int a,b; if((a=DT.findIndex(p1))<0|| (b=DT.findIndex(p2))<0) { p1=(char)br.read(); p2=(char)br.read(); p1=p2; System.out.println("((a=DT.findIndex(p1))<0||" + "(b=DT.findIndex(p2))<0)"); int t=br.read(); if(t==-1)break; else p2=(char)t; } else { a=DT.findIndex(p1); b=DT.findIndex(p2); double c=this.dist.getNum(a, b); this.dist.setNum(c*2, a, b); p1=p2; System.out.println("else"); int t=br.read(); if(t==-1)break; else p2=(char)t; } } System.out.println("mid"); int m=dist.getM(); int n=dist.getN(); for(int i=0;i<m;i++) { double tmp=0; System.out.println("mid"); for(int j=0;j<n;j++) { tmp+=dist.getNum(i, j); } double bb=1/tmp; for(int j=0;j<n;j++) { dist.setNum(dist.getNum(i, j)*bb, i, j); } System.out.print("xiedongxi"+i); } } }
You need to enable Javascript in your browser to edit pages.
help on how to format text
接着是矩阵类Matrix
最后是我们的核心类及其驱动程序
getSource.java